package com.gigi.searchcenterbackendmaster.datasource;

import cn.hutool.core.util.StrUtil;
import com.gigi.searchcenterbackendmaster.model.enums.SearchTypeEnum;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据源注册器
 *
 * @author Gigi
 * @date 2024/8/17 17:49
 */
@Component
public class DataSourceRegistry {

    @Resource
    private ArticleDataSource articleDataSource;

    @Resource
    private PictureDataSource pictureDataSource;

    @Resource
    private UserDataSource userDataSource;

    private Map<String, DataSource<T>> dataSourceMap;

    @PostConstruct // 启动时执行
    public void doInit() {
        dataSourceMap = new HashMap() {
            {
                put(SearchTypeEnum.ARTICLE.getValue(), articleDataSource);
                put(SearchTypeEnum.PICTURE.getValue(), pictureDataSource);
                put(SearchTypeEnum.USER.getValue(), userDataSource);
            }
        };
    }

    public DataSource<T> getDataSourceByType(String type) {
        if (StrUtil.isBlank(type)) {
            return null;
        }
        return dataSourceMap.get(type);
    }

}
